/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.felinelayer.client;

import br.com.felinelayer.common.Message;
import br.com.felinelayer.common.Protocol;

/**
 *
 * @author Vanius Roberto Bittencourt
 */
public abstract class ComponentClient<T extends IComponentClient> implements IComponentClient {
    private IFrameClient frame;
    private boolean inDestroy;
    private int id = 0;
    
    
    // TODO:"NON-SWING" FILDS MUST BE MOVED PARA COMPONENTCLIENT
    @Override
    public T setId( int id ) {
        this.id = id;
        return (T) this;
    }

    @Override
    public int getId() {
        return this.id;
    }

    @Override
    public final void changeProperty( Message message ) {
        if ( message.isPropertyExists( Protocol.PROPERTY_NAME_WIDTH ) ) {
            setWidht( message.getIntByName( Protocol.PROPERTY_NAME_WIDTH ) );
        }
        if ( message.isPropertyExists( Protocol.PROPERTY_NAME_LEFT ) ) {
            setLeft( message.getIntByName( Protocol.PROPERTY_NAME_LEFT ) );
        }
        if ( message.isPropertyExists( Protocol.PROPERTY_NAME_TOP ) ) {
            setTop( message.getIntByName( Protocol.PROPERTY_NAME_TOP ) );
        }
        if ( message.isPropertyExists( Protocol.PROPERTY_NAME_HEIGHT ) ) {
            setHeight( message.getIntByName( Protocol.PROPERTY_NAME_HEIGHT ) );
        }
        if ( message.isPropertyExists( Protocol.PROPERTY_NAME_TEXT ) ) {
            setText( message.getValueByName( Protocol.PROPERTY_NAME_TEXT ) );
        }
        doChangeProperty( message );
    }

    protected void doChangeProperty( Message message ) {
    }
    private Client client;

    @Override
    public final void setClient( Client client ) {
        this.client = client;
    }

    public final Client getClient() {
        return client;
    }

    protected void doDestroy() {
    }

    @Override
    public final void destroy() {
        if (inDestroy)
            return;
        inDestroy = true;
        setVisible( false );
        doDestroy();
        if (frame != null)
        frame.removeComponent( this );
        client.destroyCallBack( this );
        inDestroy = false;
    }

    @Override
    public IFrameClient getFrame() {
        return frame;
    }

    @Override
    public void setFrame( IFrameClient frame ) {
        this.frame = frame;
    }
}
